﻿
Namespace Aricie.DNN.Modules.LuceneSearch.Business

    Friend Class IUrlParametersProcessorFactory
        Private Sub New() ' default static constructor to let the compiler know it is not necessary to build a constructor
        End Sub

        Private Shared _instance As New UrlParametersProcessor
        Public Shared Function Instance() As IUrlParametersProcessor
            Return _instance
        End Function
    End Class

    Friend Interface IUrlParametersProcessor
        Function Encode(valueToEncode As String) As String
        Function Decode(valueToDecode As String) As String

        Function RetrieveParameter(parameterName As String) As String
        Function CreateParameter(parameterName As String, parameterValue As String) As String

    End Interface

    Class UrlParametersProcessor
        Implements IUrlParametersProcessor

        Protected Friend Sub New()
            ' pas de construction publique, passez par l'instanciation via la factory
            ' todo: généraliser le remplacement
            ' todo: prendre une méthode plus efficace

            'Switches.Add(New Pairing("__E", " "))
            'Switches.Add(New Pairing("__P", """"))
            'Switches.Add(New Pairing("__M", "+"))
            'Switches.Add(New Pairing("__S", "-"))
            'Switches.Add(New Pairing("__FS", "~"))
            'Switches.Add(New Pairing("__FV", "."))
            'Switches.Add(New Pairing("__OB", "["))
            'Switches.Add(New Pairing("__CB", "]"))
            'Switches.Add(New Pairing("__OC", "{"))
            'Switches.Add(New Pairing("__CC", "}"))
            'Switches.Add(New Pairing("__WS", "?"))
            'Switches.Add(New Pairing("__WM", "*"))
            'Switches.Add(New Pairing("__B", "^"))
            'Switches.Add(New Pairing("__A", "&"))
            'Switches.Add(New Pairing("__V", ","))
            'Switches.Add(New Pairing("__WD", ":"))
            'Switches.Add(New Pairing("_H_", "__"))

            'PostEncodeSwitches.Add(New Pairing("__H", "&"))
            'PostEncodeSwitches.Add(New Pairing("__D", ";"))
            'PostEncodeSwitches.Add(New Pairing("__Y", "#"))
        End Sub

        'Private Class Pairing
        '    Public Property From As String
        '    Public Property [To] As String
        '    Public Sub New(f As String, t As String)
        '        From = f
        '        [To] = t
        '    End Sub
        'End Class

        'Private Switches As New List(Of Pairing)
        'Private PostEncodeSwitches As New List(Of Pairing)

        Public Function Decode(valueToDecode As String) As String Implements IUrlParametersProcessor.Decode
            If String.IsNullOrEmpty(valueToDecode) Then
                Return valueToDecode
            End If

            Dim toreturn As String = valueToDecode
            toreturn = HttpUtility.HtmlDecode(toreturn.Replace("__H", "&").Replace("__D", ";").Replace("__Y", "#"))
            toreturn = toreturn.Replace("__E", " ")
            toreturn = toreturn.Replace("__P", """")
            toreturn = toreturn.Replace("__M", "+")
            toreturn = toreturn.Replace("__S", "-")
            toreturn = toreturn.Replace("__FS", "~")
            toreturn = toreturn.Replace("__FV", ".")
            toreturn = toreturn.Replace("__OB", "[")
            toreturn = toreturn.Replace("__CB", "]")
            toreturn = toreturn.Replace("__OC", "{")
            toreturn = toreturn.Replace("__CC", "}")
            toreturn = toreturn.Replace("__WS", "?")
            toreturn = toreturn.Replace("__WM", "*")
            toreturn = toreturn.Replace("__B", "^")
            toreturn = toreturn.Replace("__A", "&")
            toreturn = toreturn.Replace("__V", ",")
            toreturn = toreturn.Replace("__WD", ":")
            toreturn = toreturn.Replace("_H_", "__")
            Return toreturn
        End Function

        Public Function Encode(valueToEncode As String) As String Implements IUrlParametersProcessor.Encode
            If String.IsNullOrEmpty(valueToEncode) Then
                Return valueToEncode
            End If

            Dim toreturn As String = valueToEncode.Trim
            toreturn = toreturn.Replace("__", "_H_")
            toreturn = toreturn.Replace(" ", "__E")
            toreturn = toreturn.Replace("""", "__P")
            toreturn = toreturn.Replace("+", "__M")
            toreturn = toreturn.Replace("-", "__S")
            toreturn = toreturn.Replace("~", "__FS")
            toreturn = toreturn.Replace(".", "__FV")
            toreturn = toreturn.Replace("[", "__OB")
            toreturn = toreturn.Replace("]", "__CB")
            toreturn = toreturn.Replace("{", "__OC")
            toreturn = toreturn.Replace("}", "__CC")
            toreturn = toreturn.Replace("?", "__WS")
            toreturn = toreturn.Replace("*", "__WM")
            toreturn = toreturn.Replace("^", "__B")
            toreturn = toreturn.Replace("&", "__A")
            toreturn = toreturn.Replace(",", "__V")
            toreturn = toreturn.Replace(":", "__WD")
            toreturn = HttpUtility.HtmlEncode(toreturn).Replace("&", "__H").Replace(";", "__D").Replace("#", "__Y")
            Return toreturn
        End Function

        Public Function RetrieveParameter(parameterName As String) As String Implements IUrlParametersProcessor.RetrieveParameter
            Return Decode(HttpContext.Current.Request.Params(parameterName))
        End Function


        Public Function CreateParameter(parameterName As String, parameterValue As String) As String Implements IUrlParametersProcessor.CreateParameter
            Return String.Format("{0}={1}", parameterName, Encode(parameterValue))
        End Function
    End Class

End Namespace